import {TRequest} from '~/utils/ali/PCRequest'

export default class Item {
    private static mobileInfo: any = {}
    private static mobileDesc: any = {}

    public static mobileDetail(itemId: any): Promise<any> {
        return new Promise((resolve,reject) => {
            if (Object.keys(this.mobileInfo).length > 0) {
                resolve(this.mobileInfo)
            } else {
                TRequest.mobileDetail(itemId).then((res: any) => {
                    this.mobileInfo = res
                    console.log(res);
                    resolve(res)
                }).catch(e=>{
                    reject();
                })
            }
        })
    }

    public static mobileAuctionImages(itemId: any) {
        return new Promise((resolve,reject) => {
            this.mobileDetail(itemId).then((res: any) => {
                resolve(res.item.images)
            }).catch(()=>{
                reject();
            })
        })
    }

    public static mobileAuctionVideos(itemId: any) {
        return new Promise((resolve,reject) => {
            this.mobileDetail(itemId).then((res: any) => {
                const item = JSON.parse(res.apiStack[0].value).item
                resolve(item.videos || [])
            }).catch(_=>reject())
        })
    }

    public static utcItemShowList(itemId: number | string, offset: number | string, pageSize: number | string, isTmall = false) {
        return new Promise((resolve) => {
            TRequest.utcItemShowList(itemId, offset > 0 ? offset : '', pageSize, isTmall).then((res: any) => {
                resolve(res.data)
            })
        })
    }

    private static descImages(callback: Function) {
        const images: Array<string> = []
        const components = this.mobileDesc.components.componentData
        for (const component in components) {
            if (component.startsWith('detail_pic')) {
                const item = components[component]
                images.push(item.model.picUrl)
            }
        }

        callback(images)
    }

    public static shopLike(userId: any, offset: Number = 0) {
        return TRequest.shopLike(userId, offset)
    }

    public static async itemSimilar(itemId: any) {
        let data = await TRequest.request('mtop.relationrecommend.wirelessrecommend.recommend', [], '2.0', '2.7.0', {
            appId: '34850',
            params: JSON.stringify({
                tce_sid: '1891397',
                tce_vid: '0',
                count: '',
                env: 'online',
                // topic: '0_undefined',
                psId: '66687',
                pageSize: '20',
                nid: itemId,
                appid: '9709|19715|21539',
                pageNum: 1,
                // isNewMultiAcquire: 'true',
                src: 'phone'
            })
        },null,true);
        let data21539 = typeof data['data']['21539'] != 'undefined' ? data['data']['21539']['data'][0]['data'][0][0]['result'] : [];
        let data9709 = typeof data['data']['9709'] != 'undefined' ? data['data']['9709']['data'][0]['data'][0][0]['result'] : [];
        let data19715 = typeof data['data']['19715'] != 'undefined' ? data['data']['19715']['data'][0]['data'][0][0]['result'] : [];
        let dataC = data21539.concat(data9709, data19715);
        // 取出nid
        let nids = dataC.map((item: any) => {
            return item.nid;
        })
        // 去重
        nids = Array.from(new Set(nids));
        let result = [];
        for (let i = 0; i < nids.length; i++) {
            if (nids[i] != itemId) {
                // 从data中取出对应的数据
                for (let j = 0; j < dataC.length; j++) {
                    if (dataC[j].nid == nids[i]) {
                        result.push(dataC[j]);
                        break;
                    }
                }
            }
        }
        return result;
    }

    public static itemLike(itemId: any, offset: Number = 0) {
        return TRequest.itemLike(itemId, offset)
    }

    // https://shopsearch.taobao.com/search?user_id=352469034&data-value=20&sort=sale-desc&ajax=true&data-key=s&callback=callback20&callback=callback20&_=1687681757842
    public static mobileDescImages(itemId: any) {
        return new Promise((resolve) => {
            if (Object.keys(this.mobileDesc).length > 0) {
                this.descImages(resolve)
            }

            TRequest.mobileDesc(itemId).then((res: any) => {
                this.mobileDesc = res.data
                this.descImages(resolve)
            })
        })
    }
}
